
public class Play {
	public int waitCount = 3;
	public int countBeat=1;
	public int copy=100;
	public int canon=0;
	public int[] lengthArray = new int[] {0,16,20,0,21,0,0,0,0,18,0,0,0,32,7,16,16};
	public int wait=0;
	public int motifChanges=0;
	public void melody1() {
		Main.score.shimonPlaying1=1;
    	Main.score.melody1(72,16,16,2,1);
    	
	}	
	public void melody1_2() {
		Main.score.noteOut(78,0,2);
    	Main.score.melody1(84,16,16,2,1);
	}
	public void melody2() {
		Main.score.shimonPlaying1=2;
		Main.score.melody2(72,20,20,2,1);
	}

	public void melody4() {
		Main.score.shimonPlaying1=4;
		Main.score.melody4(83,21,21,2,1);
	}

	public void melody9() {
		Main.score.shimonPlaying1=9;
		Main.score.melody9(72,18,18,2,1);
	}

	public void melody11() {
			Main.score.shimonPlaying1=11;
			Main.score.noteOut(95,0,3);
	//		Main.score.noteOut(95,0,2);
			Main.score.melody11_2(72,16,16,2,1);

	}
	public void melody11_2() {

        	Main.score.noteOut(62,0,1);
			Main.score.shimonPlaying1=11;
			Main.score.noteOut(95,0,3);
			Main.score.noteOut(78,0,2);
			Main.score.melody11(60,16,16,1,2);
	}
	public void melody13() {
		Main.score.shimonPlaying1=13;
		Main.score.melody13(67,16,16,2,1);
//		Main.score.melody13(67,32,32,2,1);
	}
	public void melody14() {
		Main.score.shimonPlaying0=14;
    	Main.score.melody14(48,7,7,0,0);
	}
	public void melody14_2() {
		Main.score.shimonPlaying1=14;
		Main.score.noteOut(95,0,3);
    	Main.score.melody14(84,7,7,2,1);
	}
	public void melody15() {
		Main.score.shimonPlaying0=15;
		Main.score.melody15(48,16,16,0,0);
	}
	public void melody15_2() {
		Main.score.shimonPlaying1=15;
		Main.score.noteOut(95,0,3);
		Main.score.waitMil(2.*Main.quarter);
		Main.score.melody15(72,16,16,2,1);
	}
	public void melody16() {
		Main.score.shimonPlaying0=16;
    	Main.score.melody16(48,16,16,0,0);
	}
	public void melody16_2() {
		Main.score.shimonPlaying1=16;
    	Main.score.noteOut(72,0,2);
    	Main.score.melody16_2(84,16,16,2,1);
	}
	public void melody(int i){
		
		if (i!=Main.same0&&i!=Main.same1)
		{
//			System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! i = "+i+" same0 = "+Main.same0+" same1 = "+Main.same1);
			Main.same=false;
		}
		int stopcycle = (int) (Math.random()*100);
		if (i==Main.score.lastMotif)
		{
			Main.repeated=0;	
			// System.out.println("Main.repeated RESET!!!");
		}
		Main.score.lastMotif=i;
//    	clear the last played notes by entering garbage
    	for (int j=0;j<13;j++){
//    		System.out.println("!!!!clearing!!!!!!!!!!!!!!!!!!!!");
    		Main.notesArray[Main.notesArrayPointer][0]=j;
        	Main.notesArrayPointer=Main.notesArrayPointer+1;
    		Main.notesArray_bass[Main.notesArrayPointer_bass][0]=j;
        	Main.notesArrayPointer_bass=Main.notesArrayPointer_bass+1;
    	}
		
    	if(Main.pose) {
    		pose(0);
    		Main.pose=false;
    	}
    	if(!Main.banging) {
    		banging(1);
    		Main.banging=true;
    	}
//		stop condition
    	System.out.println("Section = "+Main.section+" motifChanges = "+motifChanges);
		if ((Main.section==3&&motifChanges==2)||(Main.section==2&&motifChanges==1))
		{
			Main.score.clearScheduler(0);
			Main.score.clearScheduler(1);
			Main.score.playing0=99;
			Main.score.playing1=99;
			Main.score.waitMil(8.*Main.quarter);			
			
		}
		
//		clear recent notes of notes array
    	for (int j=0;j<13;j++){
    		System.out.println("!!!!clearing!!!!!!!!!!!!!!!!!!!!");
    		Main.notesArray[Main.notesArrayPointer][0]=j;
        	Main.notesArrayPointer=Main.notesArrayPointer+1;
    		Main.notesArray_bass[Main.notesArrayPointer_bass][0]=j;
        	Main.notesArrayPointer_bass=Main.notesArrayPointer_bass+1;
    	}
		
//		play condition
		if(true)
		{

//			update section if necessary


			if (Main.section==3&&motifChanges>=3)
			{
				gotoSection(4);
				Main.repeated=0;
			}
			if (Main.section==2&&motifChanges>=1)
			{
				gotoSection(3);
			}
			if (Main.section==1)
			{
				gotoSection(2);
			}
			if (Main.section==0)
			{
				gotoSection(1);
			}
			
			

			
	//		wait # of cycles
			int randWait;
			if (Main.section==3&&Main.same||Main.section==7)
			{
				
				randWait=101;
			}
			else
			{
				randWait=101;

//				randWait = (int) (Math.random()*100);
			}
			if (Math.random()>.5) {
				Main.same=true;
			}
			if (true)
			{
				waitCount=0;
				
				switch(i)
				{
				case(1):
					if (Main.initTempo)
					{
						Main.quarter=(int) (countBeat/8.5);
					}
		        	
						Main.score.playing1=1;
		        	Main.score.waitMil(Main.quarter*6.8-500.);
		        	Main.score.clearScheduler(1);
		        	Main.score.clearScheduler(2);

		        	if (Main.section!=4&&Main.section!=7)
		        	{
		        		Main.score.playing0=99;
			        	Main.score.clearScheduler(1);
			        	Main.score.clearScheduler(0);	        		
		        	}
		        	Main.score.noteOut(62,0,1);
		        	Main.score.noteOut(95,0,3);
					if (Math.random()*100<copy)
					{
						Main.same=true;
						melody1();
					}
					else
					{
						if (Main.section==3||Main.section==4||Main.section==7) {
							melody1();
							Main.score.waitMil(16.*Main.quarter);
						}
			        	Main.score.clearScheduler(1);	
						Main.same=false;
						if (Math.random()<.5)
						{
							melody4();
						}
						else 
						{
							if (Main.section==4||Main.section==7)
							{
								melody16_2();
							}
							else 
							{
								melody16();
							};
						}
					}
					break;
				case(2):
					if (Main.initTempo)
					{
						Main.quarter=(int) (Main.play.countBeat/8.5);
					}
	    			Main.score.playing1=2;
					Main.score.waitMil(Main.quarter*7.7);
					Main.score.clearScheduler(1);
		        	Main.score.clearScheduler(2);

		        	if (Main.section!=4&&Main.section!=7)
		        	{
		        		Main.score.playing0=99;
			        	Main.score.clearScheduler(1);
			        	Main.score.clearScheduler(0);	        		
		        	}
					Main.score.waitMil(Main.quarter*2.-500.);
		        	Main.score.noteOut(62,0,1);
					if (Math.random()*100<copy)
					{
						Main.same=true;
						melody2();
					}
					else
					{
						if (Main.section==3||Main.section==4||Main.section==7) {
							melody2();
							Main.score.waitMil(16.*Main.quarter);
						}
			        	Main.score.clearScheduler(1);	
						Main.same=false;
						if (Math.random()<.5)
						{
							melody4();
						}
						else 
						{
							melody1();
						}
					}
					break;
				case(4):
					if (Main.initTempo)
					{
						Main.quarter=(int) (Main.play.countBeat/10.5);
					}
	    			Main.score.playing1=4;
					Main.score.waitMil(Main.quarter*5.5);
		        	Main.score.clearScheduler(1);
		        	Main.score.clearScheduler(2);

		        	if (Main.section!=4&&Main.section!=7)
		        	{
		        		Main.score.playing0=99;
			        	Main.score.clearScheduler(1);
			        	Main.score.clearScheduler(0);	        		
		        	}
					Main.score.waitMil(Main.quarter*2.);
		        	Main.score.noteOut(83,0,3);		        	
		        	Main.score.noteOut(79,0,2);
		        	Main.score.noteOut(62,0,1);
		    		Main.score.waitMil(Main.quarter*2.-500.);
					if (Math.random()*100<copy)
					{
						Main.same=true;
						melody4();
					}
					else
					{
						if (Main.section==3||Main.section==4||Main.section==7) {
							melody4();
							Main.score.waitMil(16.*Main.quarter);
						}
			        	Main.score.clearScheduler(1);
						Main.same=false;
						if (Math.random()<.5)
						{
							melody1();
						}
						else 
						{
							if (Main.section==4||Main.section==7)
							{
								melody14_2();
							}
							else 
							{
								melody14();
							}
						}
					}
					break;
				case(9):
					if (Main.initTempo)
					{
						Main.quarter=(int) (Main.play.countBeat/8.5);
					}
	    			Main.score.playing1=9;
					Main.score.waitMil(Main.quarter*6.2);
					Main.score.clearScheduler(1);
		        	Main.score.clearScheduler(2);
		        	
		        	if (Main.section!=4&&Main.section!=7)
		        	{
		        		Main.score.playing0=99;
			        	Main.score.clearScheduler(1);
			        	Main.score.clearScheduler(0);	        		
		        	}
		        	Main.score.noteOut(62,0,1);
					Main.score.waitMil(Main.quarter*2.5-500.);
					if (Math.random()*100<copy)
					{
						Main.same=true;
						melody9();
					}
					else
					{
						if (Main.section==3||Main.section==4||Main.section==7) {
							melody9();
							Main.score.waitMil(16.*Main.quarter);
						}
			        	Main.score.clearScheduler(1);
						Main.same=false;
						if (Math.random()<.5)
						{
							if (Main.section==4||Main.section==7)
							{
								melody15_2();
							}
							else 
							{
								melody15();
							}
						}
						else 
						{
							if (Main.section==4||Main.section==7)
							{
								melody16_2();
							}
							else 
							{
								melody16();
							};
						}
					}
					break;
				case(11):
					if (Main.initTempo)
					{
						Main.quarter=(int) (Main.play.countBeat/7.);
					}

						Main.score.playing1=11;

					Main.score.waitMil(Main.quarter*2.95);
					Main.score.waitMil(Main.quarter*3.);
		        	Main.score.clearScheduler(1);
		        	Main.score.clearScheduler(2);
		        	if (Main.section!=4&&Main.section!=7)
		        	{
		        		Main.score.playing0=99;
			        	Main.score.clearScheduler(1);
			        	Main.score.clearScheduler(0);	        		
		        	}
		    		Main.score.waitMil(Main.quarter*2-500.);
		        	Main.score.noteOut(62,0,1);
					if (Math.random()*100<copy)
					{
						
						if (Main.section==7)
						{
							melody11_2();
						}
						else
						{
							Main.same=true;
							melody11();
						}
					}
					else
					{
						if (Main.section==3||Main.section==4||Main.section==7) {
							melody11();
							Main.score.waitMil(16.*Main.quarter);
						}
			        	Main.score.clearScheduler(1);
						Main.same=false;
						if (Math.random()<.5)
						{
							melody4();
						}
						else 
						{
							melody9();
						}
					}
					Main.score.playing1=11;
					
					break;
				case(13):
					if (Main.initTempo)
					{
						Main.quarter=(int) (Main.play.countBeat/7.);
					}
	    			Main.score.playing1=13;
					Main.score.waitMil(Main.quarter*4.5);
		        	Main.score.clearScheduler(1);
		        	Main.score.clearScheduler(2);
		        	
		        	if (Main.section!=4&&Main.section!=7)
		        	{
		        		Main.score.playing0=99;
			        	Main.score.clearScheduler(1);
			        	Main.score.clearScheduler(0);	        
	
		        	}
					Main.score.waitMil(Main.quarter);
					Main.score.noteOut(72,0,2);	
		    		Main.score.waitMil(Main.quarter*2-500.);
		        	Main.score.noteOut(62,0,1);
					if (Math.random()*100<copy)
					{
						Main.same=true;
						melody13();
					}
					else
					{
						if (Main.section==3||Main.section==4||Main.section==7) {
							melody13();
							Main.score.waitMil(16.*Main.quarter);
						}
			        	Main.score.clearScheduler(1);
						if (Math.random()<.5)
						{
							melody11();
						}
						else 
						{
							Main.same=false;
							if (Main.section==4||Main.section==7)
							{
								melody15_2();
							}
							else 
							{
								melody15();
							}
						}
					}
					break;
				case(14):
					if (Main.initTempo)
					{
						Main.quarter=(int) (Main.play.countBeat/3.5);
					}
					Main.score.playing0=14;
					Main.score.playing1=99;
		        	Main.score.clearScheduler(0);
		        	Main.score.clearScheduler(2);
		        	
		        	if (Main.section!=4&&Main.section!=7)
		        	{
		        		Main.score.playing1=99;
			        	Main.score.clearScheduler(1);
			        	Main.score.clearScheduler(0);	        		
		        	}
		        	Main.score.noteOut(48,0,0);

		    		Main.score.waitMil(Main.quarter*1.5-500.);
		        	Main.score.noteOut(62,0,1);
					if (Math.random()*100<copy||Main.section==4)
					{
						if (Main.section==4||Main.section==7)
						{
							melody14();		
						}
						else
						{
							Main.same=true;
							melody14();
						}
					}
					else
					{
						if (Main.section==3) {
							melody14();
							Main.score.waitMil(16.*Main.quarter);
						}
			        	Main.score.clearScheduler(0);
						
						Main.same=false;
						if (Math.random()<.5)
						{
							melody2();
						}
						else 
						{
							melody9();
						}
					}
					break;
				case(15):
					if (Main.initTempo)
					{
						Main.quarter=(int) (Main.play.countBeat/7.);
					}
	    			Main.score.playing0=15;
					Main.score.playing1=99;
					Main.score.waitMil(Main.quarter*6.9);
	    			Main.score.clearScheduler(0);
	    			Main.score.noteOut(62, 0, 1);
		        	if (Main.section!=4&&Main.section!=7)
		        	{
		        		Main.score.playing1=99;
			        	Main.score.clearScheduler(1);
			        	Main.score.clearScheduler(0);	        		
		        	}
		    		Main.score.waitMil(Main.quarter*1.5-500.);
		        	Main.score.noteOut(62,0,1);
					if (Math.random()*100<copy||Main.section==4||Main.section==7||!Main.playing||Main.section==3&&!Main.same||Main.section==4&&!Main.same||Main.section==7&&!Main.same)
					{
						if (Main.section==4||Main.section==7)
						{
							melody15();		
						}
						else
						{
							Main.same=true;
							melody15();
						}
					}
					else
					{
						if (Main.section==3) {
							melody15();
							Main.score.waitMil(16.*Main.quarter);
						}
			        	Main.score.clearScheduler(0);
					
						Main.same=false;
						if (Math.random()<.5)
						{
							melody13();
						}
						else 
						{
							melody11();
						}
					}
	    			Main.score.playing0=15;
					break;
				case(16):
					if (Main.initTempo)
					{
						Main.quarter=(int) (Main.play.countBeat/3.);
					}
				Main.score.playing1=99;
		        	if (Main.section==7&&Main.score.playing0==15&&Main.score.playing1==11)
		        	{
		        		Main.score.clearScheduler(0);
		        		Main.score.clearScheduler(1);
		        		Main.score.clearScheduler(2);
		        		Main.score.noteOut(62,0,1);
		        		Main.score.playing1=99;
		        		
		        	}
		        	else
		        	{
		        		Main.score.playing0=16;
		        	}
		    		Main.score.waitMil(Main.quarter*8.95);
		        	Main.score.clearScheduler(0);
		        	Main.score.clearScheduler(2);

		        	if (Main.section!=4&&Main.section!=7)
		        	{
		        		Main.score.playing0=99;
			        	Main.score.clearScheduler(1);
			        	Main.score.clearScheduler(0);	        		
		        	}
		    		Main.score.waitMil(Main.quarter*1.);
		        	Main.score.noteOut(72,0,2);		        	
		    		Main.score.waitMil(Main.quarter*2.-500.);
	
					if (Math.random()*100<copy||Main.section==4||Main.section==7||!Main.playing||Main.section==3&&!Main.same||Main.section==4&&!Main.same||Main.section==7&&!Main.same)
					{
						if (Main.section==4||Main.section==7)
						{
							melody16();		
						}
						else
						{
							Main.same=true;
							melody16();
						}
					}
					else
					{
						if (Main.section==3) {
							melody16();
							Main.score.waitMil(16.*Main.quarter);
						}
			        	Main.score.clearScheduler(0);
					
						Main.same=false;
						if (Math.random()<.5)
						{
							melody13();
						}
						else 
						{
							if (Main.section==4||Main.section==7)
							{
								melody14_2();
							}
							else 
							{
								melody14();
							};
						}
					}
					break;
				}
				
				sendBeat(Main.quarter);
				
				motifChanges++; 
				Main.playing=true;
				if (Main.initTempo)
				{
					Main.initTempo=false;
					// System.out.println("******************INITTEMPO=FALSE");
				}		
			}
		}
	}
	
	public void sendBeat(int beat) {
		Object[] setBeat = {beat};
		OSC.main(setBeat,"/beat");
	}
	
	public void pose(int on) {
		Object[] setPose = {on};
		OSC.main(setPose,"/pose");
	}
	
	public void banging(int on) {
		Object[] setBanging = {on};
		OSC.main(setBanging,"/banging");
	}
	

	public void breathing(int on) {
		Object[] setBreathing = {on};
		OSC.main(setBreathing,"/breathing");	
	}
	public void gotoSection(int i)
	{
		switch(i)
		{
		case(0):

			Main.section=0;
			Main.sliderGUI.slider.setValue(100);
			Main.sliderGUI.slider1.setValue(0);
			Main.sliderGUI.slider2.setValue(0);
			Main.sliderGUI.slider4.setValue(0);
			Main.sliderGUI.slider5.setValue(0);
			Main.textGUI.textArea.setText("Section = "+Main.section+" \nRepeated "+Main.repeated+" times\nmotifChanges = "+Main.play.motifChanges);
			break;
		case(1):
			Main.textGUI.textArea.setText("Section = 1");
			Main.section=1;
			Main.sliderGUI.slider.setValue(100);
			Main.sliderGUI.slider1.setValue(0);
			Main.sliderGUI.slider2.setValue(0);
			Main.sliderGUI.slider4.setValue(0);
			Main.sliderGUI.slider5.setValue(0);
			Main.textGUI.textArea.setText("Section = "+Main.section+" \nRepeated "+Main.repeated+" times\nmotifChanges = "+Main.play.motifChanges);
			break;
		case(2):
			Main.textGUI.textArea.setText("Section = 2");
			Main.section=2;
			motifChanges=0;
			Main.sliderGUI.slider.setValue(100);
			Main.sliderGUI.slider1.setValue(0);
			Main.sliderGUI.slider2.setValue(0);
			Main.sliderGUI.slider4.setValue(0);
			Main.sliderGUI.slider5.setValue(0);
			Main.textGUI.textArea.setText("Section = "+Main.section+" \nRepeated "+Main.repeated+" times\nmotifChanges = "+Main.play.motifChanges);
			break;
		case(3):
			Main.textGUI.textArea.setText("Section = 3");
			Main.section=3;
			motifChanges=0;
			Main.sliderGUI.slider.setValue(0);
			Main.sliderGUI.slider1.setValue(0);
			Main.sliderGUI.slider2.setValue(0);
			Main.sliderGUI.slider4.setValue(100);
			Main.sliderGUI.slider5.setValue(0);
			Main.textGUI.textArea.setText("Section = "+Main.section+" \nRepeated "+Main.repeated+" times\nmotifChanges = "+Main.play.motifChanges);
			break;
		case(4):
			pose(1);
			Main.pose=true;
			Main.textGUI.textArea.setText("Section = 4");
			Main.section=4;
			Main.repeated=0;
			Main.sliderGUI.slider.setValue(0);
			Main.sliderGUI.slider2.setValue(0);
			Main.sliderGUI.slider1.setValue(0);
			Main.sliderGUI.slider4.setValue(0);
			Main.sliderGUI.slider5.setValue(0);
			Main.textGUI.textArea.setText("Section = "+Main.section+" \nRepeated "+Main.repeated+" times\nmotifChanges = "+Main.play.motifChanges);
			break;
		case(5):
			Main.textGUI.textArea.setText("Section = 5");
			Main.section=5;
			Main.repeated=0;
			Main.score.playing0=99;
			Main.score.playing1=99;
			Main.sliderGUI.slider.setValue(100);
			Main.sliderGUI.slider2.setValue(0);
			Main.sliderGUI.slider1.setValue(0);
			Main.sliderGUI.slider4.setValue(0);
			Main.sliderGUI.slider5.setValue(0);
			Main.textGUI.textArea.setText("Section = "+Main.section+" \nRepeated "+Main.repeated+" times\nmotifChanges = "+Main.play.motifChanges);
			break;	
		case(6):
			Main.textGUI.textArea.setText("Section = 6");
			breathing(1);
			Main.section=6;
			Main.repeated=0;
			
//        	Main.score.clearScheduler(1);
//        	Main.score.clearScheduler(0);
        	
			Main.sliderGUI.slider.setValue(100);
			Main.sliderGUI.slider2.setValue(0);
			Main.sliderGUI.slider1.setValue(0);
			Main.sliderGUI.slider4.setValue(0);
			Main.sliderGUI.slider5.setValue(0);
			Main.textGUI.textArea.setText("Section = "+Main.section+" \nRepeated "+Main.repeated+" times\nmotifChanges = "+Main.play.motifChanges);
			break;	
		case(7):
			Main.textGUI.textArea.setText("Section = 7");
			Main.section=7;
			Main.repeated=0;
			Main.sliderGUI.slider.setValue(0);
			Main.sliderGUI.slider2.setValue(0);
			Main.sliderGUI.slider1.setValue(0);
			Main.sliderGUI.slider4.setValue(0);
			Main.sliderGUI.slider5.setValue(0);
			Main.textGUI.textArea.setText("Section = "+Main.section+" \nRepeated "+Main.repeated+" times\nmotifChanges = "+Main.play.motifChanges);
			break;
		}
	}
}
